'use strict';

var app = angular.module('app', ['ui.router'])
    .config(
    [
            '$stateProvider', '$urlRouterProvider',
        function($stateProvider, $urlRouterProvider) {

            $urlRouterProvider
                .when('/sss','/demo')
                .otherwise('/demo');
                
            $stateProvider
                .state('demo', {
                    url: '/demo',
                    templateUrl: 'tpl/demo/bind.html'
                    // resolve:{
                    //     des:function(bindDemoService){
                    //       return bindDemoService.get();
                    //     }
                    // }
                })
            
                 .state('demo.demo1',{
                    url:'/demo1/:p1/:p2',
                    template:'<div><h1>{{demoText}}</h1></div>',
                    controller:'BindDemo1Ctrl'
                })
                
                .state('demo2',{
                    url:'/demo2',
                    template:'<div><h1>{{demoText}}</h1></div>'
                    
                });
        }
    ]);

app.controller('AppCtrl',[
            '$scope','demoService',
    function($scope,demoService){
        $scope.ok = function() {
           demoService.ok()
            .then(
            function(data){
                alert(data);
             }
            
            ,function(error){
                 alert('error'+error);
            }
            );
        }   
        
        $scope.$watch('length',function(newVal,oldVale){
            $scope.intArray=[];
            for(var i=0;i<$scope.length;i++){
                    $scope.intArray.push(i+1);
                } 
        });
        
        $scope.$on('$destroy',function(){
            
        });
    }
]);


app.factory('demoService',[
            '$http','$q',
    function($http,$q){
       var self={};
       
       self.ok=function () {
           
           var defer=$q.defer();
           
            $http({
                method: 'GET',
                url: 'http://localhost:10086',
                params: {
                    id: 1
                }
            }).success(function(result) {
                defer.resolve('success');
            }).error(function(x) {
                defer.reject('error');
            });
            
            return defer.promise;
       }  
       
       return self;
    }
]);
